package com.qf.agro.web.controller.device;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.sys.ResultInfo;
import com.qf.agro.common.sys.SysUserHolder;
import com.qf.agro.pojo.device.SmartDeviceDataLogs;
import com.qf.agro.pojo.facilities.FacilitiesConfig;
import com.qf.agro.service.device.SmartDeviceDataLogsService;
import com.qf.agro.service.facilities.FacilitiesConfigService;
import com.qf.agro.utils.EasyExcelUtils;
import com.qf.agro.utils.EmailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: 六六六
 * @create: 2023-07-04 14:24
 **/

@RestController
@RequestMapping("/device/dataLogs")
public class SmartDeviceDataLogsController {

    @Autowired
    private SmartDeviceDataLogsService smartDeviceDataLogsService;

    @Autowired
    private FacilitiesConfigService facilitiesConfigService;


    @GetMapping("/page")
    public ResultInfo findPage(@RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "5")Integer pageSize,
                               Long facilitiesId,Long deviceId,Integer day,Integer resultCode){
        Page<SmartDeviceDataLogs> page = smartDeviceDataLogsService.findPage(pageNum, pageSize, facilitiesId, deviceId, day, resultCode);

        return  ResultInfo.success(page);
    }
    @GetMapping("/export")
    public  void export(Long facilitiesId, Long deviceId, Integer day, Integer resultCode, HttpServletResponse response){
        List<SmartDeviceDataLogs> smartDeviceDataLogs=smartDeviceDataLogsService.findList(facilitiesId,deviceId,day,resultCode);
        EasyExcelUtils.exportExcel(smartDeviceDataLogs,SmartDeviceDataLogs.class,"智能设备日志记录","智能设备日志记录",response);
    }

    @GetMapping("/export500")
    public  void export500(Long facilitiesId, Long deviceId, Integer day, Integer resultCode, HttpServletResponse response){
        List<SmartDeviceDataLogs> smartDeviceDataLogs=smartDeviceDataLogsService.findList(facilitiesId,deviceId,day,resultCode);
        EasyExcelUtils.exportExcel(smartDeviceDataLogs,SmartDeviceDataLogs.class,"智能设备日志记录","智能设备日志记录",response);
    }

    @PostMapping("/add")
    public void addDeviceDataLogs(@RequestBody SmartDeviceDataLogs smartDeviceDataLogs) throws Exception {
        smartDeviceDataLogsService.addDeviceDataLogs(smartDeviceDataLogs);
        FacilitiesConfig facilitiesConfig = facilitiesConfigService.getFacilitiesId(smartDeviceDataLogs.getFacilitiesId());
        String[] split = smartDeviceDataLogs.getResultDataValue().split(",");
        Integer temperature = Integer.parseInt(split[0].split("=")[1]);
        Integer humidity = Integer.parseInt(split[1].split("=")[1]);
        if(temperature < facilitiesConfig.getTemperatureLow() || temperature > facilitiesConfig.getTemperatureHigh()){
            EmailUtils.send("温度出现了问题","温度异常", SysUserHolder.get().getEmail());
        }
        if(humidity < facilitiesConfig.getHumidityLow() || humidity > facilitiesConfig.getHumidityHigh()){
            EmailUtils.send("湿度出现了问题","湿度异常", SysUserHolder.get().getEmail());

        }
    }
}
